package mysql

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"month_zh5/common/nacos"
	"time"
)

var db *gorm.DB

func InitMysql() {
	var err error
	nac := nacos.NacosConfig.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		nac.Username, nac.Password, nac.Host, nac.Port, nac.Mysqlbase,
	)
	db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		return
	}
	sqlDB, err := db.DB()
	// SetMaxIdleConns sets the maximum number of connections in the idle connection pool.
	sqlDB.SetMaxIdleConns(100000)
	// SetMaxOpenConns sets the maximum number of open connections to the database.
	sqlDB.SetMaxOpenConns(1000000)
	// SetConnMaxLifetime sets the maximum amount of time a connection may be reused.
	sqlDB.SetConnMaxLifetime(time.Hour)
	err = db.AutoMigrate(&Goods{}, &Comments{}, &ShoppingCart{}, &SearchForRecords{}, &Property{}, &PropertyValue{}, &Sku{})
	if err != nil {
		return
	}
}
